package dao

import (
	"album/model"
	"context"
	"gorm.io/gorm"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
	"listenboos-common/tools"
	"time"
)

type TrackStatDao struct {
	conn *gorms.GormConn
}

func (t *TrackInfoDao) SaveTrackStat(ctx context.Context, conn db.DBConn, data *model.TrackStat) error {
	//d.conn = conn.(*gorms.GormConn)
	//tx := d.conn.Tx(ctx)
	//now := tools.ToTimeString(time.Now().UnixMilli())
	//data.CreateTime = now
	//data.UpdateTime = now
	//err := tx.Save(&data).Error
	//return data.Id, err

	//----
	t.conn = conn.(*gorms.GormConn)
	tx := t.conn.Tx(ctx)
	now := tools.ToTimeString(time.Now().UnixMilli())
	one := "0701"
	two := "0702"
	three := "0703"
	four := "0704"
	for i := 0; i < 4; i++ {
		stats := model.TrackStat{}
		stats.TrackId = data.TrackId
		stats.CreateTime = now
		stats.UpdateTime = now
		if i == 0 {
			stats.StatType = one
			err := tx.Save(&stats).Error
			if err != nil {
				return err
			}
		}
		if i == 1 {
			stats.StatType = two
			err := tx.Save(&stats).Error
			if err != nil {
				return err
			}
		}

		if i == 2 {
			stats.StatType = three
			err := tx.Create(&stats).Error
			if err != nil {
				return err
			}
		}
		if i == 3 {
			stats.StatType = four
			err := tx.Save(&stats).Error
			if err != nil {
				return err
			}
		}

	}
	return nil
}
func (d *TrackInfoDao) FindTrackStatById(ctx context.Context, tid int64) (playStatNum, collectStatNum, praiseStatNum, commentStatNum string, err error) {
	session := d.conn.Session(ctx)
	err = session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0701", tid).Select("stat_num").Scan(&playStatNum).Error
	err = session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0702", tid).Select("stat_num").Scan(&collectStatNum).Error
	err = session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0703", tid).Select("stat_num").Scan(&praiseStatNum).Error
	err = session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0704", tid).Select("stat_num").Scan(&commentStatNum).Error
	if err != nil {
		return "", "", "", "", err
	}
	return playStatNum, collectStatNum, praiseStatNum, commentStatNum, nil
}
func (d *TrackInfoDao) DeleteTrackStat(ctx context.Context, conn db.DBConn, trackId int64) error {
	data := model.TrackStat{}
	d.conn = conn.(*gorms.GormConn)
	tx := d.conn.Tx(ctx)
	now := tools.ToTimeString(time.Now().UnixMilli())
	data.UpdateTime = now
	err := tx.Model(&model.TrackStat{}).Where("track_id = ?", trackId).Update("is_deleted", 1).Error
	return err
}
func (u *TrackInfoDao) FindByTrackId(ctx context.Context, id int64) (playStatNum, collectStatNum, praiseStatNum, commentStatNum string, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0701", id).Select("stat_num").Scan(&playStatNum).Error
	err = session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0702", id).Select("stat_num").Scan(&collectStatNum).Error
	err = session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0703", id).Select("stat_num").Scan(&praiseStatNum).Error
	err = session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0704", id).Select("stat_num").Scan(&commentStatNum).Error
	if err != nil {
		return "", "", "", "", err
	}
	return playStatNum, collectStatNum, praiseStatNum, commentStatNum, nil
}
func (u *TrackInfoDao) UpdateTrackNum(ctx context.Context, aid int64, tid int64, statType string, count int64) error {
	session := u.conn.Session(ctx)
	ans := ""
	if statType == "playStatNum" {
		ans = "0701"
	}
	if statType == "collectStatNum" {
		ans = "0702"
	}
	if statType == "praiseStatNum" {
		ans = "0703"
	}
	if statType == "commentStatNum" {
		ans = "0704"
	}
	err := session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = ?", tid, ans).Updates(map[string]interface{}{
		"stat_num": gorm.Expr("stat_num + ?", count),
	}).Error
	if err != nil {
		return err
	}
	return nil
}
func (t *TrackInfoDao) UpdateCollect(trackId int64, deleted int64) error {
	session := t.conn.Session(context.Background())
	if deleted == 0 {
		err := session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0702", trackId).Updates(map[string]interface{}{
			"stat_num": gorm.Expr("stat_num + ?", 1),
		}).Error
		if err != nil {
			return err
		}
	} else if deleted == 1 {
		err := session.Model(&model.TrackStat{}).Where("track_id = ? and stat_type = 0702", trackId).Updates(map[string]interface{}{
			"stat_num": gorm.Expr("stat_num - ?", 1),
		}).Error
		if err != nil {
			return err
		}
	}
	return nil
}
func (t *TrackInfoDao) FindGeTrack(ctx context.Context, trackId int64) (ts []*model.TrackInfo, aid int64, err error) {
	session := t.conn.Session(ctx)
	err = session.Model(&model.TrackInfo{}).Where("id > ?", trackId).Limit(50).Find(&ts).Error
	for _, t := range ts {
		aid = t.AlbumId
		break
	}
	return ts, aid, err
}
func NewTrackStatDao(db *db.ListenDB) *TrackStatDao {
	return &TrackStatDao{
		conn: gorms.New(db.Conn),
	}
}
